package com.example.androidlearning2200.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.androidlearning2200.bean.BannerBean;
import com.google.gson.internal.LinkedTreeMap;

import java.util.List;

public class BannerAdapter extends PagerAdapter {

    private Context mContext;
    private List<BannerBean> mDatas;

    // 构造函数，根据参数自动生成即可
    public BannerAdapter(Context mContext, List<BannerBean> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
    }

    // 引导页有几页
    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    // 页面显示效果的逻辑
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        // 创建一个显示图片的ImageView
        ImageView imageView = new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        String imagePath = (String) mDatas.get(position).getImagePath();
        // 借助Glide获取网络图片，设置给ImageView
        Glide.with(container)
                .load(imagePath)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
        container.addView(imageView);
        return imageView;
    }

    // ViewPager每次只显示一页，不显示的页需要及时销毁，避免占用内存
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

}
